#include <iostream>
#include <sys/types.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>

using namespace std;

const char *pathname = "/home/lw/storehouse/process_communication";
const int proj_id = 0x66;

int main()
{
    // 创建key
    key_t key = ftok(pathname, proj_id);
    printf("%d\n", key);

    // 创建共享内存
    int shmid = shmget(key, 4096, IPC_CREAT | 0666);
    if(shmid < 0)
    {
        printf("shmget fail, errno is %d, errstr is %s\n", errno, strerror(errno));
        return errno;
    }
    printf("shmget success, shmid = %d\n", shmid);

    // 挂接上共享区
    char* shmaddr = (char*)shmat(shmid, nullptr, 0);
    if((long)shmaddr == -1)
    {
        printf("shmat fail, errno is %d, errstr is %s\n", errno, strerror(errno));
        return errno;
    }
    printf("shmat success\n");


    // 让我们有时间输命令，看到挂接数+1
    sleep(10);
    return 0;
}